import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

import ViewQuestionnaire
import CreateNewQuestionnaire


# 维护自增ID
markDict={}
index=0

class CreateQuestionnaireFrame(tk.Toplevel):
    def __init__(self, parent,data):
        global index
        super().__init__()

        # 初始化数据
        self.data=data

        self.parent = parent
        self.title("创建问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))



        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self, image=photo, text='返回', font=("宋体", 15), compound='left',
                                     command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01, rely=0.01, relwidth=0.15, relheight=0.1)


        # 创建 Treeview
        self.columns = ( "问卷名称")
        self.tree = ttk.Treeview(self, columns=self.columns, show="headings",height=10)

        # 定义列
        self.tree.heading("问卷名称", text="问卷名称", anchor=tk.CENTER)

        self.tree.column("问卷名称", anchor=tk.CENTER)

        # 示例数据
        # questionnaires = []



        # 添加数据到 Treeview
        for q in self.data:
            print(q)
            new_item=self.tree.insert("", tk.END, values=(q["template_name"]))
            self.tree.item(new_item, text=(q["template_name"]))
            markDict[q["template_name"]]=index
            index=index+1

        # 绑定点击事件
        self.tree.bind("<Double-1>", self.on_treeview_click)

        # 创建滚动条
        self.scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.tree.yview)
        self.tree.configure(yscroll=self.scrollbar.set)
        self.tree.place(relx=0.01, rely=0.2, relwidth=0.97, relheight=0.5)
        self.scrollbar.place(relx=0.96, rely=0.2, relwidth=0.03, relheight=0.5)

        # 创建添加
        self.add_button = tk.Button(self, text='添加', font=("宋体", 15), compound='center',
                                    command=self.open_create_new_questionnaire)
        self.add_button.place(relx=0.8, rely=0.8, relwidth=0.15, relheight=0.1)

    # 查看已经创建的问卷
    def open_questionnaire(self,event,questionnaire_id):
        print(f"Opening questionnaire with ID: {questionnaire_id}")

        # 检索出该行数据
        cl_id=markDict[questionnaire_id]

        self.open_incompleted_questionnaire_frame(self.data[cl_id])

    def open_incompleted_questionnaire_frame(self,dataByIc):
        self.withdraw()
        incompleted_questionnaire_frame = ViewQuestionnaire.ViewQuestionnaire(self,dataByIc)
        incompleted_questionnaire_frame.mainloop()
    def on_treeview_click(self,event):
        selected_item = self.tree.focus()  # 获取当前选择的行
        if selected_item:
            item = self.tree.item(selected_item)
            questionnaire_id = item['values'][0]  # 假设第一列是问卷的ID
            self.open_questionnaire(event,questionnaire_id)

    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()

    def open_create_questionnaire(self):
        self.withdraw()

        # TODO json转问答格式

        create_questionnaire = ViewQuestionnaire.ViewQuestionnaire(self,self.data)
        create_questionnaire.mainloop()

    # 创建新问卷
    def open_create_new_questionnaire(self):
        self.withdraw()
        create_new_questionnaire = CreateNewQuestionnaire.CreateNewQuestionnaire(self)
        create_new_questionnaire.mainloop()